package com.sys.user.service;

import java.util.Date;
import java.util.List;
import java.util.Optional;

import org.hibernate.service.spi.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import com.sys.api.Service.OrderUserService;
import com.sys.common.ZuelResult;
import com.sys.pojo.OrderContent;
import com.sys.pojo.OrderInfo;
import com.sys.pojo.OrderUser;
import com.sys.user.dao.OrderContentDao;
import com.sys.user.dao.OrderInfoDao;
import com.sys.user.dao.UserDao;
import com.sys.vo.CreateOrderVo;
import com.sys.vo.OrderAll;
import com.sys.vo.OrderResponse;
import com.sys.vo.OrdersList;

/**
 * @author 汪思超
 * @class 用户订单操作
 * @data 2021.4.16
 * */
@Service
@CacheConfig(cacheNames="order:user")
public class OrderUserServiceImpl implements OrderUserService {

	@Autowired
    private RedisTemplate<String, Object> redisTemplate;
	
	@Autowired
	private OrderInfoDao infoDao;
	
	@Autowired
	private OrderContentDao contentDao;
	
	@Autowired
	private UserDao userdao;
	
	
	@Override
	public ZuelResult createOrder(CreateOrderVo order) {
		// TODO Auto-generated method stub
		if (order.getTitle()==null || order.getTitle().trim().equals("")) {
			return ZuelResult.error(null, "工单标题不能为空");
		}
		if (order.getDes()==null || order.getDes().trim().equals("")) {
			return ZuelResult.error(null, "工单描述不能为空");
		}
		OrderUser user = userdao.findByUsername(order.getUname());
		if (user==null) {
			return ZuelResult.error(null, "不存在的用户名");
		}
		OrderInfo info = new OrderInfo();
		info.setMid(1);
		info.setUid(user.getId());
		info.setState("1");
		info.setTitle(order.getTitle());
		info.setDes(order.getDes());
		info.setCreateData(new Date(System.currentTimeMillis()));
		infoDao.save(info);
		redisTemplate.delete("order::getOrdersList()");
		String keyTemp = "order:user::getOrderList("+order.getUname()+")";
		redisTemplate.delete(keyTemp);
		return ZuelResult.ok(info, "创建工单成功");
	}

	@Override
	public ZuelResult responseOrder(OrderResponse res) {
		// TODO Auto-generated method stub
		OrderContent content = new OrderContent();
		if (res.getContent()==null || res.getContent().trim().equals("")) {
			return ZuelResult.error(null, "不要瞎比回复");
		}
		Optional<OrderInfo> info = infoDao.findById(res.getOrderId());
		OrderInfo orderInfo = info.get();
		if (orderInfo.getState().equals("0")) {
			return ZuelResult.error(null, "订单已经关闭");
		}
		content.setOid(res.getOrderId());
		content.setContent(res.getContent());
		content.setCreatetime(new Date(System.currentTimeMillis()));
		content.setRole(1);
		contentDao.save(content);
		String keyTemp = "order::getAllOrder(" + res.getOrderId() + ")";
		redisTemplate.delete(keyTemp);
		return ZuelResult.ok(content,"回复成功");
	}

	@Override
	public ZuelResult closeOrder(Integer  oid) {
		
		// TODO Auto-generated method stub
		Optional<OrderInfo> info = infoDao.findById(oid);
		OrderInfo info2 = info.get();
		if (info2==null) {
			throw new ServiceException("订单不存在");
		}
		info2.setState("0");
		infoDao.save(info2);
		redisTemplate.delete("order::getOrdersList()");
		return ZuelResult.ok(null, "订单已经关闭");
	}

	@Override
	@Cacheable(key="'getAllOrder('+#oid+')'")
	public OrderAll getOrders(int oid) {
		// TODO Auto-generated method stub
		Optional<OrderInfo> optional = infoDao.findById(oid);
		OrderInfo info = optional.get();
		if (info==null) {
			throw new ServiceException("工单不存在");
		}
		if (info.getState().equals("1")) {
			info.setState("进行中");
		}
		if (info.getState().equals("0")) {
			info.setState("已结束");
		}
		List<OrderContent> contents = contentDao.findByOid(oid);
		OrderAll all = new OrderAll();
		all.setInfo(info);
		all.setContents(contents);
		all.setManagerName("admin");
		Optional<OrderUser> optional2 = userdao.findById(info.getUid());
		OrderUser user = optional2.get();
		all.setUname(user.getUsername());
		return all;
	}

	@Override
	@Cacheable(key="'getOrderList('+#uname+')'")
	public OrdersList getOrderList(String uname) {
		// TODO Auto-generated method stub
		OrderUser user = userdao.findByUsername(uname);
		if (user==null) {
			throw new ServiceException("用户名不存在");
		}
		OrdersList list = new OrdersList();
		List<OrderInfo> infolist = infoDao.findByUid(user.getId());
		for (int i = 0; i < infolist.size(); i++) {
			if (infolist.get(i).getState().equals("1")) {
				infolist.get(i).setState("进行中");
			}
			if (infolist.get(i).getState().equals("0")) {
				infolist.get(i).setState("已结束");
			}
		}
		list.setInfoList(infolist);
		list.setManagerName("admin");
		return list;
	}

}
